﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using RN.ERP.BLL;
using RN.ERP.DTO;
using System.Web.Services;
using Newtonsoft.Json;
using RN.ERP.BLL.ZBDLRY;

namespace RN.ERP.Web.Material.Material
{
    public partial class MSearch : System.Web.UI.Page
    {
        MaterialBLL bll = new MaterialBLL();
        BaseBasicBLL bbll = new BasicBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMPClassData();
                Dictionary<string, string> unitDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(bbll.GetDictionary("RN_MATE_UNIT", "", ""));
                ddlUnit.DataSource = unitDic;
                ddlUnit.DataTextField = "value";
                ddlUnit.DataValueField = "key";
                ddlUnit.DataBind();
            }
        }

        protected void ddlMPClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pid = 0;
            if (int.TryParse(ddlMPClass.SelectedValue, out pid))
            {
                int count = 0;
                List<DTO.MPInfo> properties = bll.GetMPListByPropertyId(
                    pid,
                    0,
                    0,
                    out count).Where(p => p.bIsQuery == true).ToList();
                rptMPInfo.DataSource = properties;
                rptMPInfo.DataBind();
            }
        }

        protected void rptMPInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                HiddenField hdn = e.Item.FindControl("hdniMPIKind") as HiddenField;
                HiddenField hdn2 = e.Item.FindControl("hdncMPIInfo") as HiddenField;
                TextBox txt1 = e.Item.FindControl("txtSingle") as TextBox;
                TextBox txt2 = e.Item.FindControl("txtMulti") as TextBox;
                DropDownList ddl = e.Item.FindControl("ddlSelect") as DropDownList;
                CheckBoxList cb = e.Item.FindControl("cbSelect") as CheckBoxList;
                if (hdn != null)
                {
                    switch (hdn.Value.Trim())
                    {
                        case "1":
                            ddl.Visible = true;
                            if (!string.IsNullOrEmpty(hdn2.Value))
                            {
                                string[] s = hdn2.Value.Split(new char[] { '!', '#' }, StringSplitOptions.RemoveEmptyEntries);
                                ddl.DataSource = s;
                                ddl.DataBind();
                            }
                            break;
                        case "2":
                            cb.Visible = true;
                            if (!string.IsNullOrEmpty(hdn2.Value))
                            {
                                string[] s = hdn2.Value.Split(new char[] { '!', '#' }, StringSplitOptions.RemoveEmptyEntries);
                                cb.DataSource = s;
                                cb.DataBind();
                            }
                            break;
                        case "3":
                            txt1.Visible = true;
                            break;
                        case "4":
                            txt2.Visible = true;
                            break;
                        default:
                            break;

                    }
                }
            }
        }

        private void BindMPClassData()
        {
            int count = 0;
            List<DTO.MateProperty> properties = bll.GetMaterialProperties(
                0,
                0,
                out count);
            properties.Insert(0, new DTO.MateProperty() { id = 0, cMPName = "--请选择--" });
            ddlMPClass.DataSource = properties;
            ddlMPClass.DataValueField = "id";
            ddlMPClass.DataTextField = "cMPName";
            ddlMPClass.DataBind();
        }

        public string GetCondition()
        {
            bool hasCondition = false;
            DTO.QueryMaterialCondition condition = new QueryMaterialCondition();
            if (!string.IsNullOrEmpty(txtName.Value.Trim()))
            {
                hasCondition = true;
                condition.cMName = new Fly.Common.Linq.ParameterValue<string>() { O = Fly.Common.Linq.OptEnum.Like, V = txtName.Value.Trim() };
            }
            if (!string.IsNullOrEmpty(txtMNum.Value.Trim()))
            {
                int num = 0;
                if (int.TryParse(txtName.Value.Trim(), out num))
                {
                    hasCondition = true;
                    condition.iMNum = new Fly.Common.Linq.ParameterValue<int?> { O = Fly.Common.Linq.OptEnum.Equal, V = num };
                    condition.cUnit = new Fly.Common.Linq.ParameterValue<string> { O = Fly.Common.Linq.OptEnum.Equal, V = ddlMPClass.SelectedValue };
                }
            }
            if (!string.IsNullOrEmpty(txtMEval.Value.Trim()))
            {
                decimal eval = 0;
                if (decimal.TryParse(txtMEval.Value.Trim(), out eval))
                {
                    hasCondition = true;
                    condition.dMEvaluate = new Fly.Common.Linq.ParameterValue<decimal?> { O = Fly.Common.Linq.OptEnum.Equal, V = eval };
                }
            }
            Fly.Common.Linq.ParameterValues<DateTime> dCondition = new Fly.Common.Linq.ParameterValues<DateTime>() { Logic = Fly.Common.Linq.LogicEnum.And };
            bool hasDCondtion = false;
            //if (!string.IsNullOrEmpty(txtDate1.Value.Trim()))
            //{
            //    DateTime d1 = DateTime.Now;
            //    if (DateTime.TryParse(txtDate1.Value.Trim(), out d1))
            //    {
            //        hasCondition = true;
            //        dCondition.Values.Add(new Fly.Common.Linq.ParameterValue<DateTime>() { O = Fly.Common.Linq.OptEnum.GreaterThanOrEqual, V = d1 });
            //        hasDCondtion = true;
            //    }
            //}
            //if (!string.IsNullOrEmpty(txtDate2.Value.Trim()))
            //{
            //    DateTime d2 = DateTime.Now;
            //    if (DateTime.TryParse(txtDate2.Value.Trim(), out d2))
            //    {
            //        dCondition.Values.Add(new Fly.Common.Linq.ParameterValue<DateTime>() { O = Fly.Common.Linq.OptEnum.LessThanOrEqual, V = d2 });
            //        hasDCondtion = true;
            //    }
            //}
            if (hasDCondtion)
            {
                hasCondition = true;
                condition.dCDate = dCondition;
            }
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                ListItem item = CheckBoxList1.Items[i];
                if (item.Selected)
                {
                    switch (i)
                    {
                        case 0:
                            hasCondition = true;
                            condition.bIsAssets = new Fly.Common.Linq.ParameterValue<bool>() { O = Fly.Common.Linq.OptEnum.Equal, V = true };
                            break;
                        case 1:
                            hasCondition = true;
                            condition.bIsBorrow = new Fly.Common.Linq.ParameterValue<bool>() { O = Fly.Common.Linq.OptEnum.Equal, V = true };
                            break;
                        case 2:
                            hasCondition = true;
                            condition.bIsSale = new Fly.Common.Linq.ParameterValue<bool>() { O = Fly.Common.Linq.OptEnum.Equal, V = true };
                            break;
                        case 3:
                            hasCondition = true;
                            condition.bIsSample = new Fly.Common.Linq.ParameterValue<bool>() { O = Fly.Common.Linq.OptEnum.Equal, V = true };
                            break;
                        default:
                            break;
                    }
                }
            }

            if (ddlMPClass.SelectedValue != "0")
            {
                int id = 0;
                if (int.TryParse(ddlMPClass.SelectedValue, out id))
                {
                    hasCondition = true;
                    condition.cMPIid = new Fly.Common.Linq.ParameterValue<int>() { O = Fly.Common.Linq.OptEnum.Equal, V = id };
                }
            }

            if (hasCondition)
            {
                return JsonConvert.SerializeObject(condition);
            }
            else
            {
                return string.Empty;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string condition = GetCondition();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "returnToP('" + condition + "')", true);
        }
    }
}